#!/bin/bash

MIBISTRY_DIR="/var/opt/hp/hpima/registry"
SRC_MIBISTRY="/root/hpima/agents/lib/mibistry/mibistry.c"
DUMPIMREG_BIN="/opt/hp/hpima/bin/dumpimreg"

SEPERATOR="==============================================================================="
INDICATOR=">>>"

if [ ! -f "$SRC_MIBISTRY" ] ; then
	SRC_MIBISTRY="../lib/mibistry.c"
	if [ ! -f "$SRC_MIBISTRY" ] ; then
		echo "ERROR: Could not find mimbistry.c file !"
		echo "Please make sure file $SRC_MIBISTRY exists."
		exit -1
	fi
fi

if [ ! -f "$DUMPIMREG_BIN" ] ; then
	DUMPIMREG_BIN="../lib/dumpimreg"
	if [ ! -f "$DUMPIMREG_BIN" ] ; then
		echo "ERROR: Could not find dumpimreg binary file !"
		echo "Please make sure file "dumpimreg" exists."
		exit -1
	fi
fi

if [ ! -d "$MIBISTRY_DIR" ] ; then
	echo "ERROR: mibistry directory not exits !"
	echo "Please make sure directory $MIBISTRY_DIR exists."
	exit -1
fi

function search_oid_detail()
{
	OID=$1
	if [ -z "$OID" ] ; then	return -1; fi
	echo -e "Search [$OID] path in file: \n$SRC_MIBISTRY"
	cat "$SRC_MIBISTRY" | awk -F ',' 'BEGIN{ cnt = -1; }{\
		if ( cnt >= 0 ){\
			print NR,":",$0;\
			cnt = cnt + 1;\
		}\
		if ( cnt == 2 ) { cnt = -1; }\
		if ( ( $1 ~ /.*"'"$OID"'".*/)&&( cnt < 0 ) ) {\
			print NR,":",$0;\
			cnt=0;\
		}\
	}' | grep ""
	if [ "$?" -ne 0 ] ; 
	then
		echo "ERROR: Could not find this OID"
		return -1;
	fi
	return 0
}

function do_dumpimreg()
{
	FN=$1
	OID=$2
	SW=$3

	if [ ! -z "$SW" ] ; then
		if [ "$SW" = "trace" ] ; then
			echo "dumpimreg from file: $FN"
			"$DUMPIMREG_BIN" $FN | awk -F '"' '{\
				if ( $2 ~ "'"$OID"'" ) { \
					printf("\n'"$INDICATOR"' %s\n\n", $0); \
				} else { \
					print $0; \
				} \
			}'
			echo "$SEPERATOR"
		fi
	else
		SUFFIX=`echo $FN | awk -F '.' '{ print substr( $0, length($1)+1, length($0)-length($1) ) }'`
		if [ -z "$SUFFIX" ] ; then SUFFIX=".0"; fi
		"$DUMPIMREG_BIN" $FN | awk -F '"' '{ if ( $2 ~ "'"$OID"'" ) { print "'"$OID$SUFFIX ="'",substr( $0, length($2)+4) } }'
	fi
	return 0
}

function search_mibistry()
{
	OID=$1
	SW=$2
	if [ -z "$OID" ] ; then	return -1; fi
	
	if [ ! -z "$SW" ] ; then
		if [ $SW = "trace" ] ; then
			echo "$SEPERATOR"
			search_oid_detail $OID
			echo "$SEPERATOR"
			if [ $? -ne 0 ] ; then	return -1; fi
		fi
	fi

	RELA_PATH=`search_oid_detail $OID | tail -n 1 | awk -F '"' '{ print $2 }'`
	if [ -z $RELA_PATH ] ; then return -1; fi
	
	for FN in $MIBISTRY_DIR/$RELA_PATH* ;
	do
		do_dumpimreg $FN $OID $SW
	done
	echo ""
}

function list_dumpimreg()
{
	for DIRN in $MIBISTRY_DIR/* ; do
		for FN in $DIRN/* ; do
			SUFFIX=`echo $FN | awk -F '.' '{ print substr( $0, length($1)+1, length($0)-length($1) ) }'`
			if [ -z $SUFFIX ] ; then SUFFIX=".0"; fi
			"$DUMPIMREG_BIN" $FN 2>/dev/null| awk -F '"' '{ if ( NF >= 3 ) { gsub(" ", "", $2); printf ( "%s%s = %s\n", $2, "'"$SUFFIX"'",substr( $0, length($2)+4) ); } }'
		done
	done
}

SW=""
if [ "$#" -lt 1 ] ; then
	echo "Usage: command [trace|list] oid1 [oid2 oid3 ......]"
	exit -1
fi
if [ "$1" = "trace" ] ; then
	SW="trace"
	shift
elif [ "$1" = "list" ] ; then
	list_dumpimreg
	exit 0
fi

while [ "$#" -gt 0 ]; do
	search_mibistry $1 $SW
	shift
done
exit 0
